package com.yygo.webapp.controller;

import com.yygo.model.Dealer;
import com.yygo.model.DealerRule;
import com.yygo.model.LoanProduct;
import com.yygo.service.DealerManager;
import com.yygo.service.DealerRuleManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

@Controller
@RequestMapping("/dealerRuleForm")
public class DealerRuleFormController extends BaseFormController {
    private DealerRuleManager dealerRuleManager = null;

    @Autowired
    public void setDealerRuleManager(@Qualifier("dealerRuleManager") DealerRuleManager dealerRuleManager) {
        this.dealerRuleManager = dealerRuleManager;
    }

    @Autowired
    private DealerManager dealerManager;

    public DealerRuleFormController() {}

    @ModelAttribute
    @RequestMapping(method = RequestMethod.GET)
    protected DealerRule showForm(HttpServletRequest request)
    throws Exception {
        String dealerId = request.getParameter("dealerId");
        setCancelView("redirect:/dealerform?from=list&dealerId="+dealerId);
        setSuccessView("redirect:/dealerform?from=list&dealerId="+dealerId);
        String id = request.getParameter("dealerRuleId");
        DealerRule dealerRule = null;
        if (!StringUtils.isBlank(id)) {
            dealerRule = dealerRuleManager.get(new Long(id));
        } else {
            dealerRule = new DealerRule();
            dealerRule.setStatus(true);
            if (!StringUtils.isBlank(dealerId)) {
                Dealer dealer = dealerManager.get(new Long(dealerId));
                dealerRule.setDealer(dealer);
            } else {
                throw new IllegalAccessException("illegal access: dealer id is empty.");
            }
        }
        return dealerRule;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(DealerRule dealerRule, BindingResult errors, HttpServletRequest request,
                           HttpServletResponse response)
    throws Exception {
        if (dealerRule.getDealer().getId() == null) {
            throw new IllegalAccessException("illegal access: dealer id is empty.");
        } else {
            Dealer dealer = dealerManager.get(dealerRule.getDealer().getId());
            dealerRule.setDealer(dealer);
        }
        String from = request.getParameter("from");
        String success = "redirect:/dealerform?dealerId=" + dealerRule.getDealer().getId() + "&from=" + from;
        if (request.getParameter("delete") != null) {
            dealerRuleManager.remove(dealerRule.getId());
        }
        else if (request.getParameter("cancel") != null){
            return success;
        }
        else {
            dealerRuleManager.save(dealerRule);
        }
        return success;
    }
}
